﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ASPNETMVCApplication.ActionServiceReference;
using ASPNETMVCApplication.Repositories.Core;

namespace ASPNETMVCApplication.Repositories
{
    public class CategoryRepository : RepositoryBase, ICategoryRepository
    {
        /// <summary>
        /// Gets list of categories.
        /// </summary>
        /// <param name="criterion"></param>
        /// <returns></returns>
        public List<Category> GetList(Criterion criterion = null)
        {
            var request = new CategoryRequest().Prepare();
            request.LoadOptions = new string[] { "Categories" };

            CategoryResponse response = Client.GetCategories(request);

            Correlate(request, response);

            return response.Categories == null ? null : response.Categories.ToList();
        }

        public Category Get(int categoryId)
        {
            var request = new CategoryRequest().Prepare();
            request.LoadOptions = new string[] { "Category" };
            request.Criteria = new CategoryCriteria { CategoryId = categoryId };

            var response = Client.GetCategories(request);

            Correlate(request, response);

            return response.Category;
        }

        public List<Category> Search(DateTime dateFrom, DateTime dateTo, int firstPoint, int lastPoint, int supplierId, int trafficId, string sort, string order)
        {
            var request = new CategoryRequest().Prepare();

            request.LoadOptions = new string[] { "Search" };

            request.Criteria = new CategoryCriteria
            {
                DateFrom = dateFrom,
                DateTo = dateTo,
                FirstPoint = firstPoint,
                LastPoint = lastPoint,
                SupplierId = supplierId,
                TrafficId = trafficId,
                SortExpression = sort + " " + order
            };

            var response = Client.GetCategories(request);

            Correlate(request, response);

            return response.Categories.ToList();
        }

        public void Insert(Category category)
        {
            var request = new CategoryRequest().Prepare();
            request.Action = "Create";
            request.Category = category;

            var response = Client.SetCategories(request);

            Correlate(request, response);
            int id = response.Category.CategoryId;
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            
        }

        public int PostCategory(Category category)
        {
            var request = new CategoryRequest().Prepare();
            request.Action = "Create";
            request.Category = category;

            var response = Client.SetCategories(request);

            Correlate(request, response);
            int id = response.Category.CategoryId;
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return id;
        }

        public void Delete(int categoryId)
        {
            var request = new CategoryRequest().Prepare();
            request.Action = "Delete";
            request.Category = new Category { CategoryId = categoryId };

            var response = Client.SetCategories(request);

            Correlate(request, response);
        }
        
        #region Not implemented members

       

        public int GetCount(Criterion criterion = null)
        {
            throw new NotImplementedException();
        }

        

        public void Update(Category t)
        {
            throw new NotImplementedException();
        }

        

        #endregion
    }
}